﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Telerik.Web.UI;

public partial class Admin_Components_Ajax_CodeDetailList : System.Web.UI.UserControl
{
    //DatabaseHelper DB = new DatabaseHelper();
    DataTable dt = new DataTable();

    string DetailName = "CodeDetail";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetupForm();
        }
    }

    protected void SetupForm()
    {
        //Load dữ liệu cho các control khi vừa load form
        Utils.LoadDataToCombo(cboCMCODE, "CodeDetail_cbo_CmCode", DbType.String, 1);
        cboCMCODE.SelectedIndex = 0;

        //Utils.LoadDataToCombo(cboCMCODE, "CodeDetail_cbo_CmCode", DbType.String, 1);
        //cboCMCODE.SelectedIndex = 0;

        //Hiển thị thông tin các column trên lưới khi load form, lúc này chưa cần load dữ liệu lên lưới
        DataTable dt = new DataTable();
        grvData.DataSource = dt;
        grvData.DataBind();
    }

    protected void ClearForm()
    {
        cboCMCODE.SelectedIndex = 0;
        DataTable dt = new DataTable();
        grvData.DataSource = dt;
        grvData.DataBind();
    }

    private void LoadDatatoGrid()
    {
        //DB.ClearParameter();
        //DbParameter paramCMCode = DB.Parameter;

        //paramCMCode.ParameterName = "CMCode";
        //paramCMCode.DbType = DbType.String;
        //paramCMCode.Value = cboCMCODE.SelectedValue == "-1" ? "" : cboCMCODE.SelectedValue;

        //DB.AddParameter(paramCMCode);
        //dt = DB.ExecuteDataTable("CodeDetail_Search", CommandType.StoredProcedure, ConnectionState.CloseOnExit);

        DBDataContext dtx = LINQUtil.GetDBReadOnly();
        List<CodeDetail_SearchResult> listResult = dtx.CodeDetail_Search(cboCMCODE.SelectedValue == "-1" ? "" : cboCMCODE.SelectedValue).ToList();
        if (listResult.Count > 0)
        {
            //lblResult.Text = "";
            this.grvData.DataSource = listResult;
            this.grvData.DataBind();
            this.grvData.Visible = true;
        }
        else
        {
            //lblResult.Text = "<b style='color:red'>Không tìm thấy dữ liệu! </b>";
            JavascriptUtil.LoadMessage(u, false, "Không tìm thấy dữ liệu.");
            this.grvData.Visible = false;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            LoadDatatoGrid();
            btnClear.Focus();
        }
        catch (Exception ex)
        {
            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;
        }
    }
    protected void grvData_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {

        string commandName = e.CommandName;
        if (commandName == "ViewItem" || commandName == "EditDelItem")
        {
            int iRow = e.Item.ItemIndex;
            //string CMCode = cboCMCODE.SelectedValue;

            Session["CodeDetail_CMCode"] = grvData.Items[iRow]["CMCode"].Text.Replace("&nbsp;", ""); ;
            Session["CodeDetail_CDCode"] = grvData.Items[iRow]["CDCode"].Text.Replace("&nbsp;", ""); //Lấy giá trị CDCode

            if (commandName == "ViewItem")
            {
                Session[DetailName] = 2;
                RadWindow2.NavigateUrl = "CodeDetailList.aspx";
                RadWindow2.ID = "Rad1";
                RadWindow2.VisibleOnPageLoad = true;
                RadWindow2.Width = 1000;
                RadWindow2.Height = 500;
                RadWindow2.VisibleStatusbar = false;
                RadWindowManager1.Windows.Clear();
                RadWindowManager1.Windows.Add(RadWindow2);
                //ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'ViewDetail.aspx?ID=" + DetailID + "', null, 'height=500,width=1000,status=yes,toolbar=no,menubar=no,location=no' );", true);
            }
            else if (commandName == "EditDelItem")
            {
                Session["CodeDetail"] = 1; //0: tạo mới; 1: Sửa/Xóa; 2: Chỉ xem
                Response.Redirect("~/CodeDetail");
            }
        }

    }
    protected void grvData_PageIndexChanged(object sender, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        LoadDatatoGrid();
    }
    protected void grvData_PageSizeChanged(object sender, Telerik.Web.UI.GridPageSizeChangedEventArgs e)
    {
        LoadDatatoGrid();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearForm();
        btnNew.Focus();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Session[DetailName] = 0; //0: tạo mới; 1: Sửa/Xóa; 2: Chỉ xem
        Response.Redirect("~/CodeDetailList");
        btnDelSelection.Focus();
    }
    protected void btnDelSelection_Click(object sender, EventArgs e)
    {
        try
        {
            bool isDelete = false;
            foreach (GridDataItem grdItem in grvData.Items)
            {
                CheckBox chkSelection = (CheckBox)grdItem["checkbox"].Controls[0];
                if (chkSelection.Checked == true)
                {
                    isDelete = true;
                    DeleteData(grdItem.Cells[3].Text, grdItem.Cells[4].Text);
                }
            }
            if (isDelete)
                LoadDatatoGrid();
            grvData.Rebind();
        }
        catch (Exception ex)
        {
            string url = "~/Default.aspx?ID=48";
            Response.Redirect(url);
            Session["ErrorMessage"] = ex.Message;
        }
    }
    protected void DeleteData(String CMCode, String CDCode)
    {
        try
        {
            DBDataContext dtx = LINQUtil.GetDBWriteOnly();
            dtx.CodeDetail_Delete(CMCode, CDCode, Session["UserName"].ToString());
         
            JavascriptUtil.LoadMessage(u, true, "Xóa thành công!");
        }
        catch (Exception ex)
        {
            JavascriptUtil.LoadMessage(u, false, "Thông báo lỗi: Xóa không thành công.");
        }
    }
    protected void cboCMCODE_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        btnSearch.Focus();
    }
}
